import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import Components from 'unplugin-vue-components/vite'
import { VantResolver } from 'unplugin-vue-components/resolvers'
import { loadEnv } from 'vite'
import { resolve } from 'path'
import DefineOptions from 'unplugin-vue-define-options/vite'
export default defineConfig(({ mode, command }) => {
    const env = loadEnv(mode, process.cwd())
    console.log('env->', env)
    return {

        define: {
            'process.env': {}
        },
        plugins: [
            vue(),
            Components({
                resolvers: [VantResolver()],
            }),
            DefineOptions()
        ],
        resolve: {
            alias: {
                "@": resolve(__dirname, 'src'),
            },
        },
        server: {
            host: '0.0.0.0',
            port: 9483,
            proxy: {
                '/proxy': {
                    target: env.VITE_PROXY,
                    changeOrigin: true,
                    rewrite: path => path.replace(/^\/proxy/, ''),
                    bypass(req, res, options) {
                        const proxyURL = options.target + options.rewrite(req.url);
                        // console.log('proxyURL', proxyURL);
                        req.headers['x-req-proxyURL'] = proxyURL; // 设置未生效
                        res.setHeader('x-req-proxyURL', proxyURL); // 设置响应头可以看到
                    },
                },
                '/adminproxy': {
                    target: env.VITE_ADMIN_PROXY,
                    changeOrigin: true,
                    rewrite: path => path.replace(/^\/adminproxy/, ''),
                    bypass(req, res, options) {
                        const proxyURL = options.target + options.rewrite(req.url);
                        // console.log('proxyURL', proxyURL);
                        req.headers['x-req-proxyURL'] = proxyURL; // 设置未生效
                        res.setHeader('x-req-proxyURL', proxyURL); // 设置响应头可以看到
                    },
                }
            }
        }
    }

})


